//
//  MKTChatLabelValueUIView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/13.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatLabelValueUIView.h"
#import <MKUI/MKUI.h>
@interface MKTChatLabelValueUIView ()

@end

@implementation MKTChatLabelValueUIView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.label = [[UILabel alloc] init];
        self.textFont = [UIFont systemFontOfSize:12];
        self.moneyUnitFont = [UIFont systemFontOfSize:14];
        self.hintFont = [UIFont systemFontOfSize:20];
        self.normalColor = [UIColor mk_colorWithLight:[UIColor blackColor] dark:[UIColor whiteColor]];
        self.accessColor = [UIColor greenColor];
        self.warningColor = [UIColor redColor];
        [self addSubview:self.label];
    }
    return self;
}
- (void)setAllColor:(UIColor *)color{
    self.normalColor = self.accessColor = self.warningColor = color;
}
- (void)setAllFont:(UIFont *)font{
    self.textFont = self.moneyUnitFont = self.hintFont = font;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.label.frame = bounds;
}
- (CGSize)sizeThatFits:(CGSize)size{
    if(!self.labelValue){
        return CGSizeZero;
    }
    return [self.label sizeThatFits:size];
}
- (void)reloadData{
    NSString *text = self.labelValue.text ?: @"";
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    
    UIColor *color;
    switch (self.labelValue.style) {
        case MKTChatLabelValueStyleNormal:
            color = self.normalColor;
            break;
        case MKTChatLabelValueStyleAccess:
            color = self.accessColor;
            break;
        case MKTChatLabelValueStyleWarning:
            color = self.warningColor;
            break;
        default:
            break;
    }
    
    [attrString addAttributes:@{NSForegroundColorAttributeName:color} range:NSMakeRange(0, text.length)];
    
    switch (self.labelValue.format) {
        case MKTChatLabelValueFormatPlainText:
            [attrString addAttributes:@{NSFontAttributeName:self.textFont} range:NSMakeRange(0, text.length)];
            break;
        case MKTChatLabelValueFormatMoney://金额
        {//todo 使用YYLabel来实现富文本
            [attrString addAttributes:@{NSFontAttributeName:self.textFont} range:NSMakeRange(0, text.length)];
            if(text.length>=1){
                [attrString addAttributes:@{NSFontAttributeName:self.moneyUnitFont} range:NSMakeRange(0, 1)];
            }
            NSArray<NSString *> *slist = [text componentsSeparatedByString:@"."];
            if(slist.count==2){
                NSString *last = [slist lastObject];
                [attrString addAttributes:@{NSFontAttributeName:self.moneyUnitFont} range:NSMakeRange(text.length-last.length, last.length)];
            }
        }
            break;
        default:
            break;
    }
    //todo 使用YYLabel来实现富文本 hint效果
    if(self.labelValue.hint.length&&self.labelValue.format==MKTChatLabelValueFormatPlainText){
        NSString *hint = self.labelValue.hint;
        NSRange r = [text rangeOfString:hint];
        if(r.location!=NSNotFound){
            [attrString addAttributes:@{NSFontAttributeName:self.hintFont} range:r];
        }
    }
    
    self.label.attributedText = attrString;
}
@end
